// pages/collect/collect.js
import {
  getCollectList
} from '../../network/index.js';
const {
  globalData
} = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url_pre: globalData.BASE_URL,
    collectList: [],
    total: 0,
    page: 1,
    pageSize: 10,
    isOK: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {
    this.init();
  },

  //页面初始化
  init() {
    this.setData({
      isOK: false,
      collectList: [],
      total: 0,
      page: 1,
      pageSize: 10,
    });
    this.getCollectList();
  },

  async getCollectList() {
    wx.showLoading({
      title: '加载中',
    })
    await getCollectList({
      page: this.data.page,
      pageSize: this.data.pageSize
    }).then((result) => {
      const {
        items,
        total
      } = result;
      const data = items.map(item => {
        item.url = this.data.url_pre + item.urls?.split(",")[0] || "default.png";
        console.log(item.label)
        item.labels = item.label ? item.label?.split(",") : [];
        return item;
      })

      const collectList = [...this.data.collectList, ...data];
      this.setData({
        total,
        page: this.data.page++,
        collectList
      })
    })
    if (this.data.total <= this.data.collectList.length) {
      this.setData({
        isOK: true
      });
    }
    wx.hideLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isOK) {
      return;
    }
    this.getCollectList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})